import os
import shutil

def clean_fastapi_project(root_dir):
    # 定义清理规则
    patterns = {
        'dirs': ['__pycache__', '.pytest_cache', '.idea', 'tests/__pycache__'],
        'files': ['.env', '*.pyc', 'pytest.ini'],
        'exts': ['.log', '.tmp']
    }

    # 递归扫描清理
    for root, dirs, files in os.walk(root_dir):
        # 删除危险目录
        [shutil.rmtree(os.path.join(root, d)) for d in dirs 
         if any(p in d for p in patterns['dirs'])]
        
        # 删除匹配文件
        [os.remove(os.path.join(root, f)) for f in files 
         if any(f.endswith(ext) for ext in patterns['exts']) 
         or f in patterns['files']]

    # 重建标准虚拟环境
    # venv_path = os.path.join(root_dir, 'fastapi')
    # if os.path.exists(venv_path):
    #     shutil.rmtree(venv_path)
    # os.system(f'python -m venv {venv_path}')

if __name__ == "__main__":
    clean_fastapi_project(os.getcwd())